<?php
class ShippingregionAction extends CommonAction {
	function index(){
		$this->listmap='shipping_id = '.$_GET['shipping_id'];
        parent::_list();
		$list = $this->get('list');
		foreach($list as $k => $v){
			$area = M('Region') -> where("region_id in(".trim($v['regionids'], ',').")") -> select();
			foreach($area as $val){
				$list[$k]['area'] .= $val['region_name'].'　';
			}
		}
		$this -> assign('list', $list);
		$this->display();
	}
	
	public function template(){
		$info = $this->dao -> where(array('site_id' => $this->siteid, 'id' => $this->_get("id"))) -> find();
		$this->assign('info', $info);
		$this->display();
	}
	
	public function add(){
		$this -> info = M('Shipping') -> where(array('id' => $this-> _get('shipping_id'))) -> find(); 
		parent::add();
	}
	
	
	public function edit(){
		$info = $this->dao -> where(array('id' => $this -> _get('id'))) -> find();
		$info['regionids'] = trim($info['regionids'], ',');
		$this->assign('info', $info);
		$this->display();
	}
	public function update(){
		$_POST['regionids'] = ','.implode(',', $_POST['area']).',';
		parent::update(array(), U('index', array('shipping_id' => $_POST['shipping_id'])));
	}
	public function insert(){
		$_POST['regionids'] = ','.implode(',', $_POST['area']).',';
		parent::insert(array(), U('index', array('shipping_id' => $_POST['shipping_id'])));
	}
	public function getRegion(){
		$father_id = isset($_POST['fid'])?intval($_POST['fid']):0;
		if($father_id == 0){
				$datatype = ' datatype="*" ';
			}else{
				$datatype = ' ';
			}
		$db=M('Region');
		$data = $db->where(array('father_id'=>$father_id))->select();
		$str = '<option value="">-请选择-</option>';
		foreach($data as $k => $v){
			$str .= '<option value="'.$v['region_id'].'">'.$v['region_name'].'</option>';
		}
		if(count($data)>0){
			$str = '<select'.$datatype.'style="width:auto; display:inline; margin-right:8px;" class="form-control" id="select" onchange="sendRegion(this)" name="area[]">'.$str.'</select>';
		}else{
			$str='';
		}
		echo $str;
	}
	
	public function setRegion(){
		$db=M('Region');
		$area = explode(',', trim($this->_post('area'),','));
		$ret = "";
		foreach($area as $skey => $sid){
			$father_id = $skey == 0 ? 0 : $area[$skey - 1];
			$data = $db->where(array('father_id'=>$father_id))->select();
			$str = '<option value="">-请选择-</option>';
			foreach($data as $k => $v){
				$selectstr = "";
				if($sid == $v['region_id']){
					$selectstr = 'selected="selected"'; 
				}
				$str .= '<option '.$selectstr.' value="'.$v['region_id'].'">'.$v['region_name'].'</option>';
			}
			if($skey == 0){
				$datatype = ' datatype="*" ';
			}else{
				$datatype = ' ';
			}
			if(count($data)>0){
				$str = '<select'.$datatype.'style="width:auto; display:inline; margin-right:8px;" class="form-control" id="select" onchange="sendRegion(this)" name="area[]">'.$str.'</select>';
			}else{
				$str='';
			}
			$ret .= $str;
		}
		echo $ret;
	}
	public function setArea(){
		$regionids=D('shippingregion')->where('shipping_id = '.$_POST['id'])->getfield('regionids',true);
		foreach($regionids as $kk => $vv){
			$regionids[$kk] =trim($vv,',');
		}
		$regionids=implode(',',$regionids);
		if($regionids){
			$exist=' and region_id not in ('.trim($regionids,',').')';
		}else{
			$exist='';
		}
		$RegionDb=D('Region');
		$res=$RegionDb->where('father_id = 0 '.$exist)->select();
		$html='';
		foreach($res as $k => $v){
			$html.='<input id="region_'.$v['region_id'].'" type="checkbox" name="area[]" value="'.$v['region_id'].'">'.'<label style="font-weight:400" for="region_'.$v['region_id'].'"> '.$v['region_name'].'</label>　';
		}
		echo $html;
	}
	public function changeArea(){
		$selected=D('Shippingregion')->where('id = '.$_POST['id'])->getfield('regionids');
		$shippingId=D('Shippingregion')->where('id = '.$_POST['id'])->getfield('shipping_id');
		$selected=trim($selected,',');
		$html='';
		if($selected){
			$sel=D('Region')->where('father_id = 0 and region_id in ('.$selected.')')->select();
			foreach($sel as $k =>$v){
				$html.='<input id="region_'.$v['region_id'].'" type="checkbox" name="area[]" value="'.$v['region_id'].'" checked>'.'<label style="font-weight:400" for="region_'.$v['region_id'].'"> '.$v['region_name'].'</label>　';
			}
		}
		$allIn=D('Shippingregion')->where('shipping_id = '.$shippingId)->getfield('regionids',true);
		foreach($allIn as $kk => $vv){
			$allIn[$kk] =trim($vv,',');
		}
		$allIn=implode(',',$allIn);
		if($allIn){
			$exist=' and region_id not in ('.trim($allIn,',').')';
		}else{
			$exist='';
		}
		$res=D('Region')->where('father_id = 0 '.$exist)->select();
		foreach($res as $k => $v){
			$html.='<input id="region_'.$v['region_id'].'" type="checkbox" name="area[]" value="'.$v['region_id'].'">'.'<label style="font-weight:400" for="region_'.$v['region_id'].'"> '.$v['region_name'].'</label>　';
		}
		echo $html;
    }
	
} 